package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.exception.BlogException;
import com.example.demo.mapper.BlogInfoMapper;
import com.example.demo.pojo.dataobject.BlogInfo;
import com.example.demo.pojo.response.BlogInfoDetailResponse;
import com.example.demo.pojo.response.BlogInfoResponse;
import com.example.demo.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("blogService")
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper mapper;
    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper=new QueryWrapper<>();
        LambdaQueryWrapper<BlogInfo> lambda = queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, 0);
        List<BlogInfo> list = mapper.selectList(lambda);
        List<BlogInfoResponse> collect = list.stream().map(blogInfo -> {
            BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
            BeanUtils.copyProperties(blogInfo, blogInfoResponse);
            return blogInfoResponse;
        }).collect(Collectors.toList());
        log.info("collect:{}",collect);
        return collect;
    }

    @Override
    public BlogInfoDetailResponse getBlogDetail(Integer id) {
        log.info("id:{}",id);
        LambdaQueryWrapper<BlogInfo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(BlogInfo::getId,id).eq(BlogInfo::getDeleteFlag,0);
        BlogInfo blogInfo=mapper.selectOne(queryWrapper);
        if(blogInfo==null) throw new BlogException("id不存在");
        log.info("blogInfo:{}",blogInfo);
        BlogInfoDetailResponse blogInfoDetailResponse=new BlogInfoDetailResponse();
        BeanUtils.copyProperties(blogInfo,blogInfoDetailResponse);
        return  blogInfoDetailResponse;
    }
}
